﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using System.Text;
using com.loto.Common;

namespace com.huigo.Admin.Web.order
{
    public partial class OrderDetail : BasePage
    {
        public string insConpanyName = string.Empty;//保险公司
        public string insName = string.Empty;//保险名称
        public string custName = string.Empty;//经销商
        public decimal payPrice = 0;//应付单价
        public string insTime = string.Empty;//保险时间
        public string proDesc = string.Empty;//保险责任
        public string orderName = string.Empty;//订单状态
        public string insServis = string.Empty;//保险服务
        public int OrderStatusFID = 0;//订单状态
        public int PolicyStatus = 0;//保单状态
        private long sid
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["sid"], 0);
            }
        }
        private bool isModify
        {
            get
            {
                return sid == long.MinValue ? flgModify.Text == "0" ? false : true : true;
            }
        }
        private DateTime queryInvoiceStart//开始时间
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["InvoiceStart"]) ? Convert.ToDateTime(Request.QueryString["InvoiceStart"]) : DateTime.MinValue;
            }
        }
        private DateTime queryInvoiceEnd//结束时间
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["InvoiceEnd"]) ? Convert.ToDateTime(Request.QueryString["InvoiceEnd"]) : DateTime.MinValue;
            }
        }
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Bind();
        }
        int insType = 0;
        private void Bind()
        {
            if (sid > 0)
            {
               
                List<RequestFuncParams> argList = new List<RequestFuncParams>();
                argList.Add(new RequestFuncParams("sid", sid.ToString()));
                BaseService baseService = new BaseService();
                ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_ORDER_INFO_BY_ID, argList);
                if (parser != null && parser.RowsCount > 0)
                {
                    insConpanyName = parser["OrderInsCompanyName", 0].ToString();
                    custName = parser["CustName", 0].ToString();
                    insName = parser["ProductName", 0].ToString();
                    payPrice = ConvertCurrencyPay(Convert.ToDecimal(parser["OrderPayAmount", 0]));
                    insTime = parser["ProductTimeDesc", 0].ToString();
                    proDesc = parser["ProductDesc", 0].ToString();
                    orderName = parser["StatusDesc", 0].ToString();
                    insServis = parser["ProductServiceDesc", 0].ToString();
                    insType = Convert.ToInt32(parser["OrderInsTypeFid", 0].ToString());
                    int OrderStatusFid = Convert.ToInt32(parser["OrderStatusFid", 0]);
                    if (OrderStatusFid == ((int)Dict.OrderStatus.UnPay))
                    {
                      Cancel.Visible=true;//订单 等待支付
                    }
                }
                argList.Add(new RequestFuncParams("orderFid", sid.ToString()));
                string theadFormat = "<table><thead><tr><th style=\"width: 8%\"><input name=\"checkall\" onclick=\"$('input[name=group]').attr('checked',this.checked);\" type=\"checkbox\" value=\"\" />全选</th><th style=\"width: 5%\">序号</th><th style=\"width: 10%\">姓名</th><th>证件类型</th><th >证件号</th><th style=\"width: 10%\">出生日期</th><th style=\"width:5%\">性别</th><th>航班号</th><th style=\"width: 10%\">保险起期</th><th>手机号码</th><th>电子邮箱</th><th>保单号</th><th>保单状态</th></tr></thead><tbody>{0}</tbody></table>";
                string tbodyFormat = "<tr><td><input class='groupclass' name=\"group\" type=\"checkbox\" value=\"{12}\" /></td><td>{0}</td><td>{1}</td><td/>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td><div class=\"wrap_layout_track\"><a class=\"js_policy_status_history\"  href=\"javascript:void(0);\" policy_id=\"{12}\" >{11}<b class=\"arrow_piont\"></b></a><div class=\"layout_popup\"><div class=\"layout_inner order_box\"><div class=\"layout_hd\"><h4>保单操作记录</h4></div><div class=\"layout_bd\"></div></div><div class=\"layout_arrow_top\"><span class=\"\">◆</span><i>◆</i></div></div></div></td></tr>";
                StringBuilder sb = new StringBuilder();
                switch (insType)
                {
                    case (int)Dict.InsType.Air:
                        parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_POLICY_INFO_AIR_LIST, argList);
                        if (parser != null && parser.RowsCount > 0)
                        {
                            for (int i = 0; i < parser.RowsCount; i++)
                            {
                                sb.AppendFormat(tbodyFormat, i + 1, parser["PolicyInsuredName", i].ToString(), parser["PolicyInsuredCredentialType", i].ToString(), parser["PolicyInsuredCredentialNo", i].ToString(), Convert.ToDateTime(parser["PolicyInsuredBirth", i].ToString().Replace("T", " ")).ToString("yyyy-MM-dd"), parser["PolicyInsuredGender", i].ToString(), parser["PolicyFlightNo", i].ToString(), Convert.ToDateTime(parser["PolicyFromPeriod", i].ToString().Replace("T", " ")).ToString("yyyy-MM-dd"), parser["PolicyInsuredTel", i].ToString(), parser["PolicyInsuredEmail", i].ToString(), parser["PolicyNo", i].ToString(), parser["FeeStatusDesc", i].ToString() + "," + parser["StatusDesc", i].ToString(), parser["PolicyId", i].ToString());
                                if (parser["PolicyFeeStatus", i].ToString() == ((int)Dict.PolicyFeeStatus.Paid).ToString() && parser["PolicyStatus", i].ToString() == ((int)Dict.PolicyStatus.InsuredSuc).ToString())
                                {
                                    BackPro.Visible = true;//保单 投保成功|支付成功 退保
                                }
                                else if (parser["PolicyFeeStatus", i].ToString() == ((int)Dict.PolicyFeeStatus.Paid).ToString() && parser["PolicyStatus", i].ToString() == ((int)Dict.PolicyStatus.InsuredFails).ToString())
                                {
                                    BackPrice.Visible = true;//保单 投保失败且支付成功 退款
                                    BuyPro.Visible = true;//保单 投保失败且支付成功 投保
                                }
                                else if (parser["PolicyFeeStatus", i].ToString() == ((int)Dict.PolicyFeeStatus.Paid).ToString() && parser["PolicyStatus", i].ToString() == ((int)Dict.PolicyStatus.SurrenderFails).ToString())
                                {
                                    BackPrice.Visible = true;//保单 退保失败且支付成功 退款
                                }
                            }
                            litOrderList.Text = string.Format(theadFormat, sb.ToString());
                        }
                        break;
                    case (int)Dict.InsType.Travel:
                        theadFormat = "<table><thead><tr><th style=\"width: 8%\"><input name=\"checkall\" onclick=\"$('input[name=group]').attr('checked',this.checked);\" type=\"checkbox\" value=\"\" />全选</th><th style=\"width: 5%\">序号</th><th style=\"width: 10%\">姓名</th><th>证件类型</th><th >证件号</th><th style=\"width: 10%\">出生日期</th><th style=\"width:5%\">性别</th><th>航行线路</th><th style=\"width: 10%\">保险起期</th><th>手机号码</th><th>电子邮箱</th><th>保单号</th><th>保单状态</th></tr></thead><tbody>{0}</tbody></table>";
                        parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_POLICY_INFO_TRAVEL_LIST, argList);
                        if (parser != null && parser.RowsCount > 0)
                        {
                            for (int i = 0; i < parser.RowsCount; i++)
                            {
                                sb.AppendFormat(tbodyFormat, i + 1, parser["PolicyInsuredName", i].ToString(), parser["PolicyInsuredCredentialType", i].ToString(), parser["PolicyInsuredCredentialNo", i].ToString(), Convert.ToDateTime(parser["PolicyInsuredBirth", i].ToString().Replace("T", " ")).ToString("yyyy-MM-dd"), parser["PolicyInsuredGender", i].ToString(), parser["PolicyTravelLine", i].ToString(), Convert.ToDateTime(parser["PolicyFromPeriod", i].ToString().Replace("T", " ")).ToString("yyyy-MM-dd"), parser["PolicyInsuredTel", i].ToString(), parser["PolicyInsuredEmail", i].ToString(), parser["PolicyNo", i].ToString(), parser["FeeStatusDesc", i].ToString() + "," + parser["StatusDesc", i].ToString(), parser["PolicyId", i].ToString());
                                if (parser["PolicyFeeStatus", i].ToString() == ((int)Dict.PolicyFeeStatus.Paid).ToString() && parser["PolicyStatus", i].ToString() == ((int)Dict.PolicyStatus.InsuredSuc).ToString())
                                {
                                    BackPro.Visible = true;//保单 投保成功|支付成功 退保
                                }
                                else if (parser["PolicyFeeStatus", i].ToString() == ((int)Dict.PolicyFeeStatus.Paid).ToString() && parser["PolicyStatus", i].ToString() == ((int)Dict.PolicyStatus.InsuredFails).ToString())
                                {
                                    BackPrice.Visible = true;//保单 投保失败且支付成功 退款
                                    BuyPro.Visible = true;//保单 投保失败且支付成功 投保
                                }
                                else if (parser["PolicyFeeStatus", i].ToString() == ((int)Dict.PolicyFeeStatus.Paid).ToString() && parser["PolicyStatus", i].ToString() == ((int)Dict.PolicyStatus.SurrenderFails).ToString())
                                {
                                    BackPrice.Visible = true;//保单 退保失败且支付成功 退款
                                }
                            }
                            litOrderList.Text = string.Format(theadFormat, sb.ToString());
                        }

                        break;
                }
            }
        }
       //取消订单
        protected void Cancel_Click(object sender, EventArgs e)
        {
            if (CheckOrderStatus(((int)Dict.OrderStatus.UnPay).ToString()))
            {
                List<RequestFuncParams> argorderList = new List<RequestFuncParams>();
                argorderList.Add(new RequestFuncParams("OrderID", sid.ToString()));
                argorderList.Add(new RequestFuncParams("OrderStatusFid", ((int)Dict.OrderStatus.Canceled).ToString()));
                argorderList.Add(new RequestFuncParams("userID", UserId.ToString()));
                BaseService baseorderService = new BaseService();
                ParserXml parserOrder = baseorderService.RequestEntryService(Dict.ERequestFunc.UPDATE_ORDER_INFO_STATUS, argorderList);//订单
                if (parserOrder != null && parserOrder.RowsCount > 0 && parserOrder["returnargs", 0] == "1")
                {
                    GoTo(GoToType.ADDSUC);
                }
            }
            else
            {
                MessageBox.Show(this.Page, "请选择正确的订单状态！");
                return;
            }
        }
        //退保
        protected void BackPro_Click(object sender, EventArgs e)
        {
            string PolicyIDs = Request.Form["group"];
            if (!string.IsNullOrEmpty(PolicyIDs))
            {
                if (UpdatePolicyStatus(PolicyIDs, (int)Dict.UpdatePolicyStatus.RefundPolicy))//退保
                {
                    GoTo(GoToType.ADDSUC);
                }
                else
                {
                    MessageBox.Show(this.Page, "请选择正确的保单状态！");
                    return;
                }
            }
            else
            {
                MessageBox.Show(this.Page, "请选择保单！");
                return;
            }
        }
        //退款
        protected void BackPrice_Click(object sender, EventArgs e)
        {
            string PolicyIDs = Request.Form["group"];
            if (!string.IsNullOrEmpty(PolicyIDs))
            {
                if (UpdatePolicyStatus(PolicyIDs, (int)Dict.UpdatePolicyStatus.RefundPrice))//退款
                {
                    GoTo(GoToType.ADDSUC);
                }
                else
                {
                    MessageBox.Show(this.Page, "请选择正确的保单状态！");
                    return;
                }
            }
            else
            {
                MessageBox.Show(this.Page, "请选择保单！");
                return;
            }
        }
        //更新保单表数据
        private bool UpdatePolicyStatus(string PolicyIDs, int type)
        {
            List<RequestFuncParams> policyList = new List<RequestFuncParams>();
            policyList.Add(new RequestFuncParams("PolicyID", PolicyIDs));  
            policyList.Add(new RequestFuncParams("userID", UserId.ToString()));
            policyList.Add(new RequestFuncParams("type", type.ToString()));
            BaseService policyService = new BaseService();
            ParserXml policy = policyService.RequestEntryService(Dict.ERequestFunc.UPDATE_POLICY_INFO_AIR_STATUS, policyList);
            if (policy != null && policy.RowsCount > 0 && policy["returnargs", 0] == "1")
            {
                return true;
            }
            return false;
        }
        //验证订单是等待支付/支付成功
        private bool CheckOrderStatus(string orderStaus)
        {
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("cid", sid.ToString()));
            argList.Add(new RequestFuncParams("orderStaus", orderStaus));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.ORDER_INFO_IS_EXISTED, argList);
            if (parser != null && parser.RowsCount > 0 && parser["returnargs", 0] == "true")
            {
                return true;
            }
            return false;
        }
        //投保
        protected void BuyPro_Click(object sender, EventArgs e)
        {
            string PolicyIDs = Request.Form["group"];
            if (!string.IsNullOrEmpty(PolicyIDs))
            {
                if (UpdatePolicyStatus(PolicyIDs, (int)Dict.UpdatePolicyStatus.SendPolicy))//投保
                {
                    GoTo(GoToType.ADDSUC);
                }
                else
                {
                    MessageBox.Show(this.Page, "请选择正确的保单状态！");
                    return;
                }
            }
            else
            {
                MessageBox.Show(this.Page, "请选择保单！");
                return;
            }
        }

    }
}
